<?php

if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Activitymodel extends CI_Model {

    protected $_CI;

    public static $searchField = array(
        'template_id' => 'activity.template_id',
        'activity_status' => 'activity.activity_status',
        'activity_url_status' => 'activity_url.activity_url_status',
        'history_id' => 'activity_url.history_id',
        'customer_id' => 'activity_url.customer_id',
        'activity_code' => 'activity_url.activity_code',
    );
    public static $likeField = array(
      'activity_name' => 'activity_name',
      'activity_url' => 'activity_url',
      'activity_url_content' => 'activity_url_content',
      'activity_time_create_start' => 'activity_time_create >=',
      'activity_time_create_end' => 'activity_time_create <=',
    );
    public static $sortField = array(
    );

    public function __construct(){
        parent::__construct();
        $this->_CI = & get_instance();
    }
/*
| -------------------------------------------------------------------
|  GET Functions
| -------------------------------------------------------------------
*/
    public function getActivityListSection($assist){
        if(isset($assist['select'])) $this->db->select($assist['select']);
        $this->db->from('activity');
        $this->db->where('activity_status !=',STATUS_DELETE);
        // if(isset($assist['join_customer'])) {
        //   $this->db->join('customer','customer.customer_id = history.customer_id','left');
        // }
        if(isset($assist['join_user'])) {
          $this->db->join('user','user.user_id = activity.user_id','left');
        }
        if(isset($assist['where'])) $this->db->where($assist['where']);
        if(isset($assist['like'])) $this->db->like($assist['like']);
        if (isset($assist['where_in']) && !empty($assist['where_in'])) {
            foreach ($assist['where_in'] as $k => $v) {
                if(!empty($v) && is_array($v)) $this->db->where_in($k,$v);
                else {
                    $query = $this->db->get();
                    if(isset($assist['nocount'])) return array();
                    return array(array(),0);
                }
            }
        }
        if(!isset($assist['nocount'])) $count = $this->db->count_all_results('',SQL_EXECUTE_RETAIN_CONDITION);

        // $this->db->order_by('activity_status','desc');
        // $this->db->order_by('activity_sort','desc');
        $this->db->order_by('activity_id','desc');
        if(isset($assist['pagination'])) $this->db->limit($assist['pagination']['limit'],$assist['pagination']['start']);
        $query = $this->db->get();
        $list = $query->result_array();

        if(isset($assist['nocount'])) return $list;
        return array($list,$count);
    }

    public function getActivityUrlListSection($assist){
        if(isset($assist['select'])) $this->db->select($assist['select']);
        $this->db->from('activity_url');
        $this->db->where('activity_url_status !=',STATUS_DELETE);
        if(isset($assist['join_activity'])) {
          $this->db->join('activity','activity.activity_id = activity_url.activity_id','left');
        }
        if(isset($assist['join_history'])) {
          $this->db->join('history','history.history_id = activity_url.history_id','left');
        }
        if(isset($assist['join_customer'])) {
          $this->db->join('customer','customer.customer_id = activity_url.customer_id','left');
        }
        if(isset($assist['where'])) $this->db->where($assist['where']);
        if(isset($assist['like'])) $this->db->like($assist['like']);
        if (isset($assist['where_in']) && !empty($assist['where_in'])) {
            foreach ($assist['where_in'] as $k => $v) {
                if(!empty($v) && is_array($v)) $this->db->where_in($k,$v);
                else {
                    $query = $this->db->get();
                    if(isset($assist['nocount'])) return array();
                    return array(array(),0);
                }
            }
        }
        if(!isset($assist['nocount'])) $count = $this->db->count_all_results('',SQL_EXECUTE_RETAIN_CONDITION);

        // $this->db->order_by('activity_status','desc');
        // $this->db->order_by('activity_sort','desc');
        $this->db->order_by('activity_url_id','desc');
        if(isset($assist['pagination'])) $this->db->limit($assist['pagination']['limit'],$assist['pagination']['start']);
        $query = $this->db->get();
        $list = $query->result_array();

        if(isset($assist['nocount'])) return $list;
        return array($list,$count);
    }


    public function getActivitySection($assist){
        if(isset($assist['select'])) $this->db->select($assist['select']);
        $this->db->from('activity');
        if(isset($assist['where'])) $this->db->where($assist['where']);
        if(isset($assist['like'])) $this->db->like($assist['like']);
        $this->db->limit(1);
        $query = $this->db->get();
        $res = $query->row_array();

        return $res;
    }

    public function getActivity($activity_id){
        $this->db->from('activity');
        $this->db->where('activity_id',$activity_id);
        $this->db->limit(1);
        $query = $this->db->get();
        $res = $query->row_array();

        return $res;
    }

    public function getActivityByCode($activity_code){
        $this->db->from('activity');
        $this->db->where('activity_code',$activity_code);
        $this->db->limit(1);
        $query = $this->db->get();
        $res = $query->row_array();

        return $res;
    }

    public function getActivityUrlByCode($activity_url_code){
        $this->db->from('activity_url');
        $this->db->where('activity_url_code',$activity_url_code);
        $this->db->limit(1);
        $query = $this->db->get();
        $res = $query->row_array();

        return $res;
    }


    public function getActivityById($activity_id){
        if(!is_array($activity_id)) $activity_id = array($activity_id);
        if(empty($activity_id)) return array();

        $this->db->from('activity');
        $this->db->where_in('activity_id',$activity_id);
        $query = $this->db->get();
        $list = $query->result_array();

        return $list;
    }

    public function getActiveActivityList() {
      $this->db->from('activity');
      $this->db->where('activity_status !=',STATUS_DELETE);
      $query = $this->db->get();
      $list = $query->result_array();

      return $list;
    }

/*
| -------------------------------------------------------------------
|  CREATE Functions
| -------------------------------------------------------------------
*/
    public function createActivity($info){
        $db_debug = $this->db->db_debug;
        // $this->db->db_debug = FALSE;
        $m = $this->db->insert('activity',$info);
        $id = $this->db->insert_id();
        $this->db->db_debug = $db_debug;
        if($m) $this->db->query('insert into {PRE}activity_history select null,a.* from {PRE}activity as a where activity_id='.$id);

        return $m ? $id : 0;
    }

    public function createActivityUrl($info){
      $db_debug = $this->db->db_debug;
      // $this->db->db_debug = FALSE;
      $m = $this->db->insert('activity_url',$info);
      $id = $this->db->insert_id();
      $this->db->db_debug = $db_debug;

      return $m ? $id : 0;
    }

/*
| -------------------------------------------------------------------
|  EDIT Functions
| -------------------------------------------------------------------
*/
    public function editActivity($info){
        if(isset($info['activity_id'])) {
            $activity_id = $info['activity_id'];
            unset($info['activity_id']);
            $this->db->where('activity_id',$activity_id);
            $this->db->update('activity',$info);
            $this->db->query('insert into {PRE}activity_history select null,a.* from {PRE}activity as a where activity_id='.$activity_id);
        }
    }

    public function editActivityUrl($info){
        if(isset($info['activity_url_id'])) {
            $activity_url_id = $info['activity_url_id'];
            unset($info['activity_url_id']);
            $this->db->where('activity_url_id',$activity_url_id);
            $this->db->update('activity_url',$info);
        }
    }

    public function checkActivityJoin($customer_id,$activity_id) {
      $this->db->from('activity_url');
      $this->db->where('customer_id',$customer_id);
      $this->db->where('activity_id',$activity_id);
      $this->db->limit(1);
      $query = $this->db->get();
      $res = $query->row_array();

      return !empty($res);
    }
}
